<template>
    <div>
        <!-- TypeNav 组件为全局组件，所以不需要再引入 引入的是TypeNav 组件的name-->
        <TypeNavIndex></TypeNavIndex>
        <!-- 商品列表 开始 -->
        <!--list-content-->
        <div class="mainList">
            <div class="py-container">
                <!--bread-->
                <div class="bread">
                    <ul class="fl sui-breadcrumb">
                        <li>
                            <a href="#">全部结果</a>
                        </li>
                    </ul>
                    <!-- 面包屑 -->
                    <ul class="fl sui-tag">
                        <!-- 分类面包屑 -->
                        <li class="with-x" v-if="searchParams.categoryName">{{ searchParams.categoryName }}<i
                                @click="removeCategoryName">×</i></li>
                        <!-- 关键字面包屑 -->
                        <li class="with-x" v-if="searchParams.keyword">{{ searchParams.keyword }}<i
                                @click="removeKeyword">×</i></li>
                        <!-- 品牌面包屑 trademark:"6:VIVO" -->
                        <li class="with-x" v-if="searchParams.trademark"> {{ searchParams.trademark.split(":")[1] }}<i
                                @click="removeTrademark">×</i></li>
                        <!-- 品牌面包屑 trademark:"{__ob__: Observer} '安卓手机'" -->
                        <li class="with-x" v-for="(attr, index) in searchParams.props" :key="index"> {{
                                attr.split(":")[1]
                        }}<i @click="removeAttr(index)">×</i></li>


                    </ul>
                </div>

                <!-- 品牌 -->
                <!-- 通过自定义事件，接收 “子” 传来的数据 -->
                <SearchSelector @trademarkInfo="trademarkInfo" @attrInfo="attrInfo" />

                <!--details-->
                <div class="details clearfix">
                    <!-- 分类 -->
                    <div class="sui-navbar">
                        <div class="navbar-inner filter">
                            <ul class="sui-nav">
                                <!-- 'order': '1:desc', 1为综合，2为价格；当满足时给其class属性 -->
                                <li :class="{ active: isOne }" @click="changeOrder('1')">
                                    <a href="#">综合
                                        <span v-show="isOne">{{ isDesc ? "⬇" : "⬆" }}</span>
                                    </a>
                                </li>
                                <li :class="{ active: isTwo }" @click="changeOrder(('2'))">
                                    <a href="#">价格
                                        <span v-show="isTwo">{{ isDesc ? "⬇" : "⬆" }}</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <!-- 商品细节 -->
                    <div class="goods-list">
                        <ul class="yui3-g">

                            <li class="yui3-u-1-5" v-for="(good, index) in goodsList" :key="index">
                                <div class="list-wrap">
                                    <div class="p-img">
                                        <router-link :to="`/detail/${good.id}`" target="_blank"><img v-lazy="good.defaultImg" /></router-link>
                                    </div>
                                    <div class="price">
                                        <strong>
                                            <em>¥ </em>
                                            <i>{{ good.price }}</i>
                                        </strong>
                                    </div>
                                    <div class="attr">
                                        <a target="_blank" href="item.html"
                                            title="促销信息，下单即赠送三个月CIBN视频会员卡！【小米电视新品4A 58 火爆预约中】">{{ good.title }}</a>
                                    </div>
                                    <div class="commit">
                                        <i class="command">已有<span>{{ good.id }}</span>人评价</i>
                                    </div>
                                    <div class="operate">
                                        <router-link  :to="`/detail/${good.id}`" target="_blank"
                                            class="sui-btn btn-bordered btn-danger">加入购物车</router-link>
                                        <a href="javascript:void(0);" class="sui-btn btn-bordered">收藏</a>
                                    </div>
                                </div>
                            </li>

                        </ul>
                    </div>

                    <!-- 分页 -->
                    <PageinatonIndex :pageNo="searchParams.pageNo" :pageSize="searchParams.pageSize" :total="total" :continues="5" @getPageNo="getPageNo" />
                </div>

                <!--hotsale-->
                <div class="clearfix hot-sale">
                    <h4 class="title">热卖商品</h4>
                    <div class="hot-list">
                        <ul class="yui3-g">
                            <li class="yui3-u-1-4">
                                <div class="list-wrap">
                                    <div class="p-img">
                                        <img src="./images/search/like1.png" />
                                    </div>
                                    <div class="attr">
                                        <em>Apple苹果iPhone 6s (A1699)</em>
                                    </div>
                                    <div class="price">
                                        <strong>
                                            <em>¥</em>
                                            <i>4088.00</i>
                                        </strong>
                                    </div>
                                    <div class="commit">
                                        <i class="command">已有700人评价</i>
                                    </div>
                                </div>
                            </li>
                            <li class="yui3-u-1-4">
                                <div class="list-wrap">
                                    <div class="p-img">
                                        <img src="./images/search/like3.png" />
                                    </div>
                                    <div class="attr">
                                        <em>金属A面，360°翻转，APP下单省300！</em>
                                    </div>
                                    <div class="price">
                                        <strong>
                                            <em>¥</em>
                                            <i>4088.00</i>
                                        </strong>
                                    </div>
                                    <div class="commit">
                                        <i class="command">已有700人评价</i>
                                    </div>
                                </div>
                            </li>
                            <li class="yui3-u-1-4">
                                <div class="list-wrap">
                                    <div class="p-img">
                                        <img src="./images/search/like4.png" />
                                    </div>
                                    <div class="attr">
                                        <em>256SSD商务大咖，完爆职场，APP下单立减200</em>
                                    </div>
                                    <div class="price">
                                        <strong>
                                            <em>¥</em>
                                            <i>4068.00</i>
                                        </strong>
                                    </div>
                                    <div class="commit">
                                        <i class="command">已有20人评价</i>
                                    </div>
                                </div>
                            </li>
                            <li class="yui3-u-1-4">
                                <div class="list-wrap">
                                    <div class="p-img">
                                        <img src="./images/search/like2.png" />
                                    </div>
                                    <div class="attr">
                                        <em>Apple苹果iPhone 6s (A1699)</em>
                                    </div>
                                    <div class="price">
                                        <strong>
                                            <em>¥</em>
                                            <i>4088.00</i>
                                        </strong>
                                    </div>
                                    <div class="commit">
                                        <i class="command">已有700人评价</i>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- 商品列表 结束 -->

    </div>
</template>

<script>
// 这里使用mapGetters 简化了mapState
import SearchSelector from './SearchSelector.vue'
import { mapGetters,mapState } from 'vuex'
export default {
    name: 'ListIndex',
    components: {
        SearchSelector,
    },
    data() {
        return {
            // 带给服务器的参数
            searchParams: {
                'category1Id': '',
                'category2Id': '',
                'category3Id': '',
                'categoryName': '',
                'keyword': '',
                'order': '2:asc',
                'pageNo': 1,  //当前页
                'pageSize': 10, // 一页多少条数据
                'props': [],
                'trademark': ''
            }
        }
    },
    computed: {
        // 这里用了命名空间，所有mapGetters 有划分模块；若没有使用直接可以使用数组的方式调用
        ...mapGetters({
            goodsList: 'listData/goodsList',
        }),
        ...mapState({
        total: function (state) {
        return state.listData.listData.total;
      },
        }),
        // 判断排序
        isOne() {
            return this.searchParams.order.includes('1');
        },
        isTwo() {
            return this.searchParams.order.includes('2');
        },
        isDesc() {
            return this.searchParams.order.includes('desc')
        },
    },
    beforeMount() {
        // 复杂的写法
        // this.searchParams.category1Id = this.$route.query.category1Id;
        // this.searchParams.category2Id = this.$route.query.category2Id;
        // this.searchParams.category3Id = this.$route.query.category3Id;
        // this.searchParams.categoryName = this.$route.query.categoryName;
        // this.searchParams.keyword = this.$route.params.keyword;
        // 把this.$route.params/query 传给 searchParams
        Object.assign(this.searchParams, this.$route.params, this.$route.query)
    },

    // 组件挂载完毕执行一次，所以调用方法需要让数据改变就更新，不能咏mounted
    mounted() {
        this.getData();

    },
    // 监听组件身上属性的属性值变化
    watch: {
        // 如果发生变化再次发生请求
        $route(oldValue, newValue) {
            // 发送之前还需要整理下请求参数
            Object.assign(this.searchParams, this.$route.query, this.$route.params)
            // 再次发送ajax请求
            this.getData()
            // 请求完毕后，清空所有的categoryId
            // this.$route.query.category1Id = '',
            // this.$route.query.category2Id = '',
            // this.$route.query.category3Id = ''
        }
    },
    methods: {
        getData() {
            // 向服务器发送请求获取数据，参数不同返回来的数据不同，不传的时候也需要给一个空对象{}
            this.$store.dispatch("listData/getList", this.searchParams)
        },
        // ×掉面包屑中的分类
        removeCategoryName() {
            // 当值为undefined时，还会传递参数；当为undifind时则不会，提高了传输效率
            // this.searchParams.categoryName = '';

            this.searchParams.categoryName = undefined;
            // 与之对象的categoryId也需要删除
            this.searchParams.category1Id = undefined,
                this.searchParams.category2Id = undefined,
                this.searchParams.category3Id = undefined
            // 删除后 这里需要刷新数据
            this.getData();
            // 地址栏修改直接跳到自己的路由
            if (this.$route.params) {
                this.$router.push({ name: "listName", params: this.$route.params })
            }
        },
        // ×掉面包屑中的keyword 这里存在问题，当keyword 被删除后，categoryName不会删除但是 categoryId 会被删除！！
        removeKeyword() {
            this.searchParams.keyword = undefined;
            // 删除后 这里需要刷新数据
            this.getData();
            //发出通知header 处理自定义事件clean 清空搜索框中的value
            this.$bus.$emit("clean")

            // 地址栏修改直接跳到自己的路由  

            if (this.$route.query) {
                this.$router.push({ name: "listName", query: this.$route.query });
            }
            // if (this.$route.query) {
            //     this.$router.push({ name: "listName", query: this.$route.query })
            // }

        },
        //自定义事件回调
        trademarkInfo(trademark) {
            //整理请求参数  格式为 ID:品牌名称   模板字段的书写格式
            this.searchParams.trademark = `${trademark.tmId}:${trademark.tmName}`;
            //再次发请求获取search模块列表数据进行展示
            this.getData();
        },
        //删除品牌的信息
        removeTrademark() {
            //将品牌信息置空
            this.searchParams.trademark = '';
            //再次发请求
            this.getData();
        },
        //收集平台属性回调函数（自定义事件）
        attrInfo(attr, attrValue) {
            //属性ID：属性值：属性名
            //整理参数格式
            let props = `${attr.attrId}:${attrValue}:${attr.attrName}`;
            //数组去重
            if (this.searchParams.props.indexOf(props) == -1) {
                this.searchParams.props.push(props);
            }
            //再次发送请求
            this.getData();
        },
        //删除售卖属性
        removeAttr(index) {
            //再次整理参数
            this.searchParams.props.splice(index, 1);
            //再次发请求
            this.getData();
        },
        //排序的操作
        changeOrder(flag) {
            //flag形参是一个标记，代表用户点击的是综合还是价格
            let originOrder = this.searchParams.order;
            //获取初始状态
            let originFlag = this.searchParams.order.split(":")[0];
            let originSort = this.searchParams.order.split(":")[1];
            let newOrder = "";
            // 字段样式 'order': '2:asc',
            if (flag == originFlag) {
                newOrder = `${originFlag}:${originSort == "desc" ? "asc" : "desc"}`;
            } else {
                newOrder = `${flag}:${"desc"}`;
            }
            //将新的order赋值给searchParams
            this.searchParams.order = newOrder;
            //再次发送请求
            this.getData();
        },
        // 自定义事件的回调函数 --获取当前的页数
        getPageNo(pageNo){
            // 整理参数
            this.searchParams.pageNo = pageNo;
            // 重新发请求
            this.getData();
        }
    }
}
</script>

<style scoped lang="css">
.mainList {
    margin: 10px 0;
}

.mainList .py-container {
    width: 1200px;
    margin: 0 auto;
}

.mainList .py-container .bread {
    margin-bottom: 5px;
    overflow: hidden;
}

.mainList .py-container .bread .sui-breadcrumb {
    padding: 3px 15px;
    margin: 0;
    font-weight: 400;
    border-radius: 3px;
    float: left;
}

.mainList .py-container .bread .sui-breadcrumb li {
    display: inline-block;
    line-height: 18px;
}

.mainList .py-container .bread .sui-breadcrumb li a {
    color: #666;
    text-decoration: none;
}

.mainList .py-container .bread .sui-breadcrumb li a:hover {
    color: #4cb9fc;
}

.mainList .py-container .bread .sui-tag {
    margin-top: -5px;
    list-style: none;
    font-size: 0;
    line-height: 0;
    padding: 5px 0 0;
    margin-bottom: 18px;
    float: left;
}

.mainList .py-container .bread .sui-tag .with-x {
    font-size: 12px;
    margin: 0 5px 5px 0;
    display: inline-block;
    overflow: hidden;
    color: #000;
    background: #f7f7f7;
    padding: 0 7px;
    height: 20px;
    line-height: 20px;
    border: 1px solid #dedede;
    white-space: nowrap;
    transition: color 400ms;
    cursor: pointer;
}

.mainList .py-container .bread .sui-tag .with-x i {
    margin-left: 10px;
    cursor: pointer;
    font: 400 14px tahoma;
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

.mainList .py-container .bread .sui-tag .with-x:hover {
    color: #28a3ef;
}



.mainList .py-container .details {
    margin-bottom: 5px;
}

.mainList .py-container .details .sui-navbar {
    overflow: visible;
    margin-bottom: 0;
}

.mainList .py-container .details .sui-navbar .filter {
    min-height: 40px;
    padding-right: 20px;
    background: #fbfbfb;
    border: 1px solid #e2e2e2;
    padding-left: 0;
    border-radius: 0;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);
}

.mainList .py-container .details .sui-navbar .filter .sui-nav {
    position: relative;
    left: 0;
    display: block;
    float: left;
    margin: 0 10px 0 0;
}

.mainList .py-container .details .sui-navbar .filter .sui-nav li {
    float: left;
    line-height: 18px;
}

.mainList .py-container .details .sui-navbar .filter .sui-nav li a {
    display: block;
    cursor: pointer;
    padding: 11px 15px;
    color: #777;
    text-decoration: none;
}

.mainList .py-container .details .sui-navbar .filter .sui-nav li.active a {
    background: #e1251b;
    color: #fff;
}

.mainList .py-container .details .goods-list {
    margin: 20px 0;
}

.mainList .py-container .details .goods-list ul {
    display: flex;
    flex-wrap: wrap;
}

.mainList .py-container .details .goods-list ul li {
    height: 100%;
    width: 20%;
    margin-top: 10px;
    line-height: 28px;
}

.mainList .py-container .details .goods-list ul li .list-wrap .p-img {
    padding-left: 15px;
    width: 215px;
    height: 255px;
}

.mainList .py-container .details .goods-list ul li .list-wrap .p-img a {
    color: #666;
}

.mainList .py-container .details .goods-list ul li .list-wrap .p-img a img {
    max-width: 100%;
    height: auto;
    vertical-align: middle;
}

.mainList .py-container .details .goods-list ul li .list-wrap .price {
    padding-left: 15px;
    font-size: 18px;
    color: #c81623;
}

.mainList .py-container .details .goods-list ul li .list-wrap .price strong {
    font-weight: 700;
}

.mainList .py-container .details .goods-list ul li .list-wrap .price strong i {
    margin-left: -5px;
}

.mainList .py-container .details .goods-list ul li .list-wrap .attr {
    padding-left: 15px;
    width: 85%;
    overflow: hidden;
    margin-bottom: 8px;
    min-height: 38px;
    cursor: pointer;
    line-height: 1.8;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

.mainList .py-container .details .goods-list ul li .list-wrap .attr a {
    color: #333;
    text-decoration: none;
}

.mainList .py-container .details .goods-list ul li .list-wrap .commit {
    padding-left: 15px;
    height: 22px;
    font-size: 13px;
    color: #a7a7a7;
}

.mainList .py-container .details .goods-list ul li .list-wrap .commit span {
    font-weight: 700;
    color: #646fb0;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate {
    padding: 12px 15px;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate .sui-btn {
    display: inline-block;
    padding: 2px 14px;
    box-sizing: border-box;
    margin-bottom: 0;
    font-size: 12px;
    line-height: 18px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    border-radius: 0;
    background-color: transparent;
    margin-right: 15px;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate .btn-bordered {
    min-width: 85px;
    background-color: transparent;
    border: 1px solid #8c8c8c;
    color: #8c8c8c;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate .btn-bordered:hover {
    border: 1px solid #666;
    color: #fff !important;
    background-color: #666;
    text-decoration: none;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate .btn-danger {
    border: 1px solid #e1251b;
    color: #e1251b;
}

.mainList .py-container .details .goods-list ul li .list-wrap .operate .btn-danger:hover {
    border: 1px solid #e1251b;
    background-color: #e1251b;
    color: white !important;
    text-decoration: none;
}


.mainList .py-container .hot-sale {
    margin-bottom: 5px;
    border: 1px solid #ddd;
}

.mainList .py-container .hot-sale .title {
    font-weight: 700;
    font-size: 14px;
    line-height: 21px;
    border-bottom: 1px solid #ddd;
    background: #f1f1f1;
    color: #333;
    margin: 0;
    padding: 5px 0 5px 15px;
}

.mainList .py-container .hot-sale .hot-list {
    padding: 15px;
}

.mainList .py-container .hot-sale .hot-list ul {
    display: flex;
}

.mainList .py-container .hot-sale .hot-list ul li {
    width: 25%;
    height: 100%;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .p-img,
.mainList .py-container .hot-sale .hot-list ul li .list-wrap .price,
.mainList .py-container .hot-sale .hot-list ul li .list-wrap .attr,
.mainList .py-container .hot-sale .hot-list ul li .list-wrap .commit {
    padding-left: 15px;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .p-img img {
    max-width: 100%;
    vertical-align: middle;
    border: 0;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .attr {
    width: 85%;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    margin-bottom: 8px;
    min-height: 38px;
    cursor: pointer;
    line-height: 1.8;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .price {
    font-size: 18px;
    color: #c81623;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .price strong {
    font-weight: 700;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .price strong i {
    margin-left: -5px;
}

.mainList .py-container .hot-sale .hot-list ul li .list-wrap .commit {
    height: 22px;
    font-size: 13px;
    color: #a7a7a7;
}
</style>